package com.leantech.android.browser.ui

import android.content.Context
import android.content.res.Configuration
import android.os.Bundle
import com.leantech.android.browser.skin.SkinManager
import com.qmuiteam.qmui.arch.QMUIFragmentActivity
import com.qmuiteam.qmui.skin.QMUISkinManager
import com.qmuiteam.qmui.util.QMUIStatusBarHelper

open class BaseFragmentActivity: QMUIFragmentActivity() {

    private val onSkinChangeListener =
        QMUISkinManager.OnSkinChangeListener { _, _, newSkin ->
            if (newSkin == SkinManager.SkinDark) {
                QMUIStatusBarHelper.setStatusBarDarkMode(this)
            } else {
                QMUIStatusBarHelper.setStatusBarLightMode(this)
            }
        }

    override fun attachBaseContext(newBase: Context?) {
        val fontSizeConfig = Configuration(newBase?.resources?.configuration)
        fontSizeConfig.fontScale = 1.0f
        applyOverrideConfiguration(fontSizeConfig)
        super.attachBaseContext(newBase)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        skinManager = QMUISkinManager.defaultInstance(this)
        onSkinChangeListener.onSkinChange(skinManager, -1, skinManager.currentSkin)
    }

    override fun onStart() {
        super.onStart()
        skinManager?.addSkinChangeListener(onSkinChangeListener)
    }

    override fun onStop() {
        super.onStop()
        skinManager?.removeSkinChangeListener(onSkinChangeListener)
    }

    override fun finish() {
        super.finish()
        overridePendingTransition(
            com.qmuiteam.qmui.arch.R.anim.slide_still, com.qmuiteam.qmui.arch.R.anim.slide_out_right
        )
    }

}